<?php
/**
 * game.php
 * 
 * Written by: A. Sari
 * $Id: game.php 20 2009-08-30 08:40:47Z ahmtsari $
 * 
 */

include_once('team.php');

class Game
{
	var $id;
	var $day;
	var $date;
	var $home_team;
	var $away_team;	
	var $home_score;
	var $away_score;
	var $prediction;
	
	function Game($id = NULL) {
		if (isset($id))
			$this->set($id);
	}
	
	function set($id) {
		global $database;
		$this->id = $id;
		$result = $database->cursor("SELECT * FROM games WHERE id=$this->id");
        if($database->numrows < 1)
	        return; //Indicates username failure
		
		/* Retrieve result */
	    $row = $database->dbarray["0"];
		$this->id = $row['id'];
		$this->date = $row["game_date"];
		$this->home_score = $row['home_team_score'];
		$this->away_score = $row['away_team_score'];
		$this->day = $row['day'];
		$t = new Team(0, "", "");
		$t->set($row['home_team_id']);
		$this->home_team = $t;
		unset($t);
		$t = new Team(0, "", "");
		$t->set($row['away_team_id']);
		$this->away_team = $t;
	}
	
	function getList($leagueId = 0) {
		global $database;
		$q = "SELECT * FROM games";
		if ($leagueId > 0) 
			$q .= " WHERE league_id = $leagueId";
		$q .= " ORDER BY game_date DESC;";
		return $database->cursor($q);
	}
	
	function getListComplete($leagueId = 0) {
		global $database, $session;
		$q = "SELECT * FROM games WHERE game_date < '" . date('Y-m-d H:i:s', $session->gameTime) . "'";
		$q .= " AND (home_team_score IS NULL OR home_team_score IS NULL)";
		if ($leagueId > 0) 
			$q .= " AND league_id = $leagueId";
		$q .= " ORDER BY game_date DESC;";
		return $database->cursor($q);
	}

	function getListCompleted($leagueId = 0) {
		global $database, $session;
		$q = "SELECT * FROM games WHERE game_date < '" . date('Y-m-d H:i:s', $session->gameTime) . "'";
		if ($leagueId > 0) 
			$q .= " AND league_id = $leagueId";
		$q .= " ORDER BY game_date DESC;";
		return $database->cursor($q);
	}
	
	function getListOpen($leagueId = 0) {
		global $database, $session;
		$q = "SELECT * FROM games WHERE game_date > '" . date('Y-m-d H:i:s', $session->gameTime) . "'";
		if ($leagueId > 0) 
			$q .= " AND league_id = $leagueId";
		$q .= " ORDER BY game_date DESC;";
		return $database->cursor($q);
	}	
	
	
	function upsert($id, $date, $home_team_id, $away_team_id, $day, $leagueId) {
		global $database;
		$this->id = $id;
		if (isset($id)) {
			$q = "UPDATE games";
			$q .= " SET game_date = '$date', home_team_id = $home_team_id, away_team_id = $away_team_id";
			$q .= " ,day = $day";
			$q .= " WHERE id=$this->id;";
		}
		else {
			$q = "INSERT INTO games (league_id, day, game_date, home_team_id, away_team_id)";
			$q .= " VALUES ($leagueId, $day, '$date', $home_team_id, $away_team_id);";
		}
		return $database->query($q);		
	}
	
	function delete() {
		global $database;
		$database->query("DELETE FROM predictions WHERE game_id=$this->id;");
		return $database->query("DELETE FROM games WHERE id=$this->id;");	
	}
	
	function resultUpdate() {
		global $database;
		
		$q = "UPDATE games SET home_team_score = ";
		if (isset($this->home_score) && $this->home_score != "") 
			$q .=  $this->home_score;
		else
			$q .=  "NULL";
			
		$q .= ", away_team_score = ";
		if (isset($this->away_score)&& $this->away_score != "") 
			$q .=  $this->away_score;
		else
			$q .=  "NULL";
		$q .= " WHERE id=$this->id;";
		return $database->query($q);		
	}	
	
	function calculateUser($username = NULL) {
		//active user		
		$this->prediction = new Prediction(0, $username);
		$this->prediction->setPrediction($this->id);	
		$pts = 0;
		//echo "<p>1. " . $pts;
		if ($this->home_score == $this->prediction->home_score)
			$pts += 1;
		//echo "<p>2. " . $pts;
		if ($this->away_score == $this->prediction->away_score)
			$pts += 1;		
		//echo "<p>3. " . $pts;	
		if ($this->gameResult($this->home_score, $this->away_score) == 
			$this->gameResult($this->prediction->home_score, $this->prediction->away_score))
			$pts += 3;
		//echo "<p>4. " . $pts;	
		if ($this->home_score == $this->prediction->home_score
			&& $this->away_score == $this->prediction->away_score)
			$pts += 5;		
		//echo "<p>5. " . $pts;	

		$this->prediction->pts = $pts;
		return $this->prediction->updatePts();	
	}
	
	function gameResult($home, $away) {
		$result = $home - $away;
		$gameResult = 0;
		if ($result > 0)
			$gameResult = 1;
		else if ($result < 0)
			$gameResult = 2;
		return $gameResult;
	}
	
	function getNextGame($xgame_id = NULL) {
		global $database, $session;
		
		unset($this->id);
		$q = "Select id, game_date FROM games ";
		$q .= " WHERE game_date > '" . date('Y-m-d H:i:s', $session->gameTime) . "'";
		$q2 = "";
		if (isset($xgame_id))
			$q2 .= " AND id > $xgame_id";
		$q3 = " ORDER BY game_date, id";
		$result = $database->cursor($q . $q2 . $q3);
        if($database->numrows < 1)
	        $result = $database->cursor($q . $q3);
	    if($database->numrows < 1) {
	    	$this->set(0);
	    	return;
	    }
	    	
		/* Retrieve result */
	    $row = $database->dbarray["0"];
		$id = $row['id'];
		$this->set($id);
		return $q . $q2 . $q3;
	}
	
	function getStat($game_id = NULL) {
		global $database;
		$q = "Select home_score, away_score, count(*) as count FROM predictions";
		if (isset($game_id))
			$q .= " WHERE game_id=$game_id";
		else
			$q .= " WHERE game_id=$this->id";			
		$q .= " GROUP BY home_score, away_score ORDER BY count DESC";	
		return $database->cursor($q);
	}	
}
?>
